package com.yundong.gongchengji.ui.mian;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yundong.gongchengji.R;
import com.yundong.gongchengji.base.BaseActivity;
import com.yundong.gongchengji.base.BaseFragment;
import com.yundong.gongchengji.ui.mian.fragment.HomeFragment;
import com.yundong.gongchengji.ui.mian.fragment.MeFragment;
import com.yundong.gongchengji.ui.mian.fragment.TaskFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    private RadioGroup radioGroup;
    private TextView textView;
    private ViewPager viewPager;
    //fragment集合
    private List<BaseFragment>listFragment;
    //按钮集合
    private List<RadioButton>lisgRadioButton;
    private RadioButton btnHome;
    private RadioButton btnTask;
    private RadioButton btnHuyihu;
    private RadioButton btnMe;

    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        radioGroup= (RadioGroup) findViewById(R.id.group);
        viewPager= (ViewPager) findViewById(R.id.view_pager);
        listFragment=new ArrayList<BaseFragment>();
        lisgRadioButton=new ArrayList<RadioButton>();
        btnHome= (RadioButton) findViewById(R.id.btn_home);
        btnTask= (RadioButton) findViewById(R.id.btn_task);
        btnHuyihu= (RadioButton) findViewById(R.id.btn_huyihu);
        btnMe= (RadioButton) findViewById(R.id.btn_me);

    }

    @Override
    protected void initVariable() {

        listFragment.add(new HomeFragment());
        listFragment.add(new TaskFragment());
        listFragment.add(new HomeFragment());
        listFragment.add(new MeFragment());

        lisgRadioButton.add(btnHome);
        lisgRadioButton.add(btnTask);
        lisgRadioButton.add(btnHuyihu);
        lisgRadioButton.add(btnMe);

        btnHome.setOnClickListener(this);
        btnTask.setOnClickListener(this);
        btnHuyihu.setOnClickListener(this);
        btnMe.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getCount() {
                return listFragment.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                lisgRadioButton.get(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_home:
                btnHome.setChecked(true);
                viewPager.setCurrentItem(0);
                break;
            case R.id.btn_task:
                btnTask.setChecked(true);
                viewPager.setCurrentItem(1);
                break;
            case R.id.btn_huyihu:
                btnHuyihu.setChecked(true);
                viewPager.setCurrentItem(2);
                break;
            case R.id.btn_me:
                btnMe.setChecked(true);
                viewPager.setCurrentItem(3);
                break;
        }
    }
}
